package com.eastone.user.serverapi.controller;

import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.eastone.user.serverapi.model.InfoRoleAuth;
import com.eastone.user.serverapi.service.InfoRoleAuthService;
import com.yjx.framework.cache.ServiceResultCodeCache;
import com.yjx.framework.model.ServiceResult;
import com.yjx.framework.request.YjxHttpServletRequest;
import com.yjx.framework.util.JsonUtil;

@Controller
@RequestMapping(value = "/infoRoleAuth")
public class InfoRoleAuthController {

	private static final Logger LOG = LogManager.getLogger(InfoRoleAuthController.class);
	private static ServiceResultCodeCache resultCode = ServiceResultCodeCache.getInstance();
	@Autowired
	private InfoRoleAuthService infoRoleAuthService;

	@RequestMapping(value = "/getList", method = RequestMethod.GET)
	public @ResponseBody JSONObject getList(YjxHttpServletRequest req) {
		String roleId = req.getStr("roleId");
		if (LOG.isDebugEnabled()) {
			LOG.debug("get(YjxHttpServletRequest req)");
			LOG.debug("roleId=" + roleId);
		}

		List<InfoRoleAuth> infoRoleAuths = infoRoleAuthService.getByRoleId(roleId);
		JSONObject jsonObject = JsonUtil.getSuccessJsonObject();
		jsonObject.put("data", JSONArray.toJSON(infoRoleAuths));
		return jsonObject;
	}


	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public @ResponseBody JSONObject add(YjxHttpServletRequest req) {
		String roleId = req.getStr("roleId");
		String authId = req.getStr("authId");
		String desc = req.getStr("desc");
		if (LOG.isDebugEnabled()) {
			LOG.debug("update(YjxHttpServletRequest req)");
			LOG.debug("roleId=" + roleId);
			LOG.debug("authId=" + authId);
			LOG.debug("desc=" + desc);
		}

		InfoRoleAuth infoRoleAuth = new InfoRoleAuth();
		infoRoleAuth.setRoleId(roleId);
		infoRoleAuth.setAuthId(authId);
		infoRoleAuth.setDesc(desc);
		ServiceResult<String> serviceResult = infoRoleAuthService.saveOrUpdate(infoRoleAuth);
		if (serviceResult.getCode().equals(resultCode.get("common.sucess"))) {
			return JsonUtil.getSuccessJsonObject();
		}
		return JsonUtil.getFailJsonObject();
	}
	
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
	public @ResponseBody JSONObject delete(YjxHttpServletRequest req, @PathVariable("id") int id) {
		if (LOG.isDebugEnabled()) {
			LOG.debug("id=" + id);
		}

		InfoRoleAuth infoRoleAuth = new InfoRoleAuth();
		infoRoleAuth.setId(id);
		ServiceResult<String> serviceResult = infoRoleAuthService.delete(infoRoleAuth);
		if (serviceResult.getCode().equals(resultCode.get("common.sucess"))) {
			return JsonUtil.getSuccessJsonObject();
		}
		return JsonUtil.getFailJsonObject();
	}

}
